<template>
	<view class="equipment">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				设备维护
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop clearfix" :style="{marginTop: pHeight + tHeight + 'px'}">
			<view class="left fl"></view>
			<!-- <view class="right fr" @click="topage('/subpackageA/equipment/installrecord')">
				<view class="name">安装回收记录</view>
			</view> -->
		</view>
		<view class="nr">
			<view class="list">
				<view class="liebiao clearfix">
					<view class="left fl">设备编号：</view>
					<view class="right fl">
						<view class="title">{{device.charger_number}}</view>
					</view>
				</view>
				<view class="liebiao clearfix">
					<view class="left fl">设备名称：</view>
					<view class="right fl">
						<view class="title">{{device.charger_name}}</view>
					</view>
				</view>
				<view class="liebiao clearfix" v-if="device.type == 1">
					<view class="left fl">设备状态：</view>
					<view class="right fl">
						<view class="title"> <text v-if="device.charger_runstatus==1">在线</text> <text v-else>不在线</text> </view>
					</view>
				</view>
				<view class="liebiao clearfix" v-if="device.type == 1">
					<view class="left fl">信号值：</view>
					<view class="right fl">
						<view class="title">{{device.charger_signal}}</view>
					</view>
				</view>
			</view>
			<view class="tanbao clearfix" @click="yjcaozuo">
				<view class="name fl">一键弹宝</view>
				<image :src="baseUrl + '/wximage/tbtubiao.png'" class="fl"></image>
			</view>
		</view>
		<view class="mendian">
			<view class="heads clearfix">
				<view class="left fl">
					<image :src="baseUrl + '/wximage/hktubiao.png'"></image>
					<view class="name">充电宝数量：{{list.length}}</view>
				</view>
				<view class="right fr" @click="deviceInfo">
					<image :src="baseUrl + '/wximage/gengxin.png'"></image>
					<view class="name">同步仓位情况</view>
				</view>
			</view>
			<view class="shuju clearfix" v-if="list.length != 0">
				<view class="li" v-for="(item,index) in list" :key="index">
					<view class="shang clearfix">
						<view class="left fl">
							<image :src="baseUrl + '/wximage/shuzibj.png'"></image>
							<view class="shuzi">{{item.port}}</view>
						</view>
						<view class="title fl">{{item.battery_sn}}</view>
					</view>
					
					<view class="dianliang">电量： 
						<text v-if="item.battery_electricity==1">20</text>
						<text v-if="item.battery_electricity==2">30</text>
						<text v-if="item.battery_electricity==3">40</text>
						<text v-if="item.battery_electricity==4">50</text>
						<text v-if="item.battery_electricity==5">60</text>
						<text v-if="item.battery_electricity==6">70</text>
						<text v-if="item.battery_electricity==7">80</text>
						<text v-if="item.battery_electricity==8">90</text>
						<text v-if="item.battery_electricity==9">100</text>
					</view>
					
					<view class="xia clearfix">
						<view class="jincang" v-if="item.power==0" @click="discang(item.charger_number,item.port,1)">禁仓</view>
						<view class="jincang" v-if="item.power==1" @click="discang(item.charger_number,item.port,0)">解除</view>
						
						<view class="tanchu" v-if="item.bt_stat==1" @click="tanchu(item.charger_number,item.port)">弹出</view>
						<view class="wu" v-else-if="item.bt_stat==0">
							无
						</view>
						<view v-else class="guzhang">
							故障
						</view>
					</view>
				</view>
			</view>
			<view class="empty" v-else>
				<image :src="baseUrl + '/wximage/empty.png'"></image>
				<text>暂无充电宝数据</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				list: [],
				formData:{
					sn: '',
				},
				device:{}
			}
		},
		onLoad(options) {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			this.formData.sn = options.deviceid;
			this.deviceInfo();
		},
		methods: {
			tanchu(sn,port){
				this.formData = {
					sn: sn,
					port: port
				};
				var that = this;
				this.request({
					url: '/wxdevice/openport',
					data: this.formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						
						//显示加载框
						uni.showLoading({
							title: '弹出中'
						});
						
						setTimeout(function() {
							uni.showToast({
								title: "弹出成功",
								icon: 'none'
							});
							//隐藏加载框
							uni.hideLoading();
							
							that.deviceInfo();
							
						}, 3000);
						
						// console.log(this.deviceList);
						// var userId = res.data.data.id;//创建的商户id
						// uni.setStorageSync('bussiness_id',userId);
						// uni.setStorageSync('puhuobussiness_id',userId);
						// uni.setStorageSync('puhuobuzhou',2);
						// uni.redirectTo({
						// 	url: '/subpackageA/device/store'
						// })
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
						//隐藏加载框
						uni.hideLoading();
					}
				})
			},
			//一键弹宝
			yjcaozuo(sn){
				var that = this;
				this.request({
					url: '/wxdevice/openallport',
					data: this.formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						// this.bussinessList = res.data.data.list;
						// uni.showToast({
						// 	title: "弹出成功",
						// 	icon: 'none'
						// })
						//显示加载框
						uni.showLoading({
							title: '弹出中'
						});
						setTimeout(function() {
							uni.showToast({
								title: "弹出成功",
								icon: 'none'
							});
							//隐藏加载框
							uni.hideLoading();
							that.deviceInfo();
						}, 3000);
						// console.log(this.deviceList);
						// var userId = res.data.data.id;//创建的商户id
						// uni.setStorageSync('bussiness_id',userId);
						// uni.setStorageSync('puhuobussiness_id',userId);
						// uni.setStorageSync('puhuobuzhou',2);
						// uni.redirectTo({
						// 	url: '/subpackageA/device/store'
						// })
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
						//隐藏加载框
						uni.hideLoading();
					}
				})
			},
			discang(sn,port,power){
				var formData = {
					sn: sn,
					port: port,
					power: power
				};
				this.request({
					url: '/wxdevice/offport',
					data: formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						// this.bussinessList = res.data.data.list;
						if(power==1){
							uni.showToast({
								title: "禁用成功",
								icon: 'none'
							})
						}else{
							uni.showToast({
								title: "解禁成功",
								icon: 'none'
							})
						}
						this.deviceInfo();
						// console.log(this.deviceList);
						// var userId = res.data.data.id;//创建的商户id
						// uni.setStorageSync('bussiness_id',userId);
						// uni.setStorageSync('puhuobussiness_id',userId);
						// uni.setStorageSync('puhuobuzhou',2);
						// uni.redirectTo({
						// 	url: '/subpackageA/device/store'
						// })
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			},
			//设备详情信息
			deviceInfo(){
				this.request({
					url: '/wxdevice/deviceinfo',
					data: this.formData,
					method: "GET"
				}).then(res => {
					if (res.data.code == 200) {
						if(res.data.data==null){
							this.list = [];
						}else{
							this.list = res.data.data.list;
							this.device = res.data.data.device;
						}
						console.log(this.list);
						// console.log(this.deviceList);
						// var userId = res.data.data.id;//创建的商户id
						// uni.setStorageSync('bussiness_id',userId);
						// uni.setStorageSync('puhuobussiness_id',userId);
						// uni.setStorageSync('puhuobuzhou',2);
						// uni.redirectTo({
						// 	url: '/subpackageA/device/store'
						// })
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			},
			
			topage(url){
				uni.navigateTo({
					url
				})
			},
			fanhui() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.equipment {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			padding: 10rpx 40rpx 0;
			box-sizing: border-box;

			.right {
				width: 180rpx;
				height: 36rpx;
				line-height: 36rpx;
				text-align: center;
				background: #FFFFFF;
				border-radius: 24rpx;

				.name {
					font-size: 24rpx;
					color: #333333;
				}
			}
		}
		
		.nr{
			width: 90%;
			margin: 0 auto;
			z-index: 70;
			margin-top: -280rpx;
			box-shadow: rgba(223, 230, 255, 0.67) 0px 0px 6px;
			position: relative;
			
			.list{
				width: 100%;
				background: #fff;
				border-radius: 20rpx;
				margin-bottom: 20rpx;
				padding: 40rpx 40rpx 10rpx;
				
				.liebiao{
					margin-bottom: 30rpx;
					
					.left{
						font-size: 24rpx;
						color: #333333;
						width: 30%;
					}
					
					.right{
						
						.title{
							font-size: 24rpx;
							color: #333333;
							
							text{
								font-size: 24rpx;
								color: #333333;
							}
						}
						
						.name{
							font-size: 24rpx;
							color: #5C88FA;
						}
					}
				}
			}
			
			.tanbao{
				position: absolute;
				right: 20rpx;
				bottom: 90rpx;
				
				.name{
					font-size: 24rpx;
					color: #333333;
					margin-right: 8rpx;
				}
				
				image{
					width: 32rpx;
					height: 32rpx;
					margin-top: -4rpx;
				}
			}
		}
		
		.mendian{
			width: 90%;
			margin: 0 auto;
			z-index: 70;
			margin-top: 30rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-shadow: rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.heads{
				width: 100%;
				padding: 24rpx 20rpx 0;
				box-sizing: border-box;
				
				.left{
					
					image{
						float: left;
						width: 40rpx;
						height: 40rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 28rpx;
						color: #333333;
						float: left;
					}
				}
				
				.right{
					
					image{
						float: left;
						width: 34rpx;
						height: 34rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 24rpx;
						color: #368BF8;
						float: left;
					}
				}
			}
			
			.shuju{
				width: 100%;
				padding: 30rpx 26rpx;
				box-sizing: border-box;
				
				.li{
					width: 48%;
					background: #F6F8FF;
					border-radius: 16rpx;
					padding: 16rpx 20rpx 20rpx;
					box-sizing: border-box;
					float: left;
					margin-right: 4%;
					margin-bottom: 24rpx;
					
					.shang{
						position: relative;
						
						.left{
							
							image{
								width: 32rpx;
								height: 32rpx;
							}
							
							.shuzi{
								position: absolute;
								top: 0;
								left: 0;
								width: 32rpx;
								text-align: center;
								height: 32rpx;
								line-height: 32rpx;
								color: #FFFFFF;
							}
						}
						
						.right{
							font-size: 24rpx;
							color: #FA5E5C;
						}
						
						.title{
							font-size: 24rpx;
							color: #333333;
							margin-left: 10rpx;
						}
					}
					
					.dianliang{
						font-size: 24rpx;
						color: #333333;
						margin-left: 34rpx;
						margin-top: 6rpx;
					}
					
					.xia{
						margin-top: 14rpx;
						margin-left: 28rpx;
						
						.jincang{
							width: 90rpx;
							height: 32rpx;
							text-align: center;
							line-height: 28rpx;
							font-size: 22rpx;
							color: #3B91F8;
							border: 1rpx solid #3B91F8;
							border-radius: 16rpx;
							display: inline-block;
						}
						
						.tanchu{
							width: 90rpx;
							height: 32rpx;
							text-align: center;
							line-height: 28rpx;
							font-size: 22rpx;
							color: #FFFFFF;
							border: 1rpx solid #3B91F8;
							border-radius: 16rpx;
							background: #3B91F8;
							display: inline-block;
							margin-left: 24rpx;
						}
						.wu{
							width: 90rpx;
							height: 32rpx;
							text-align: center;
							line-height: 28rpx;
							font-size: 22rpx;
							color: #FFFFFF;
							border: 1rpx solid #3B91F8;
							border-radius: 16rpx;
							background: #3B91F8;
							display: inline-block;
							margin-left: 24rpx;
						}
						.guzhang{
							width: 90rpx;
							height: 32rpx;
							text-align: center;
							line-height: 28rpx;
							font-size: 22rpx;
							color: #FFFFFF;
							border: 1rpx solid #f80b02;
							border-radius: 16rpx;
							background: #f80b02;
							display: inline-block;
							margin-left: 24rpx;
						}
						
						.kongcang{
							width: 90rpx;
							height: 32rpx;
							text-align: center;
							line-height: 28rpx;
							font-size: 22rpx;
							color: #FFFFFF;
							border: 1rpx solid #707070;
							border-radius: 16rpx;
							background: #707070;
							display: inline-block;
							margin-left: 24rpx;
						}
					}
				}
				
				.li:nth-of-type(2n){
					margin-right: 0;
				}
			}
		}
		
		.empty{
			width: 100%;
			margin: 20rpx auto 0;
			text-align: center;
			padding-bottom: 20rpx;
			
			image{
				width: 390rpx;
				height: 340rpx;
			}
			
			text{
				display: block;
				margin-top: 30rpx;
				font-size: 24rpx;
				color: #666666;
			}
		}
	}
</style>